package org.link.zuoye1;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * 
 * project_name:ZuoYe
 * package_name:org.link.zuoye1
 * file_name:Test1.java
 * @author Link
 * create_date:2015年8月10日 下午4:13:32
 * modified_date:
 * description: 输入一个整数转换成相应的二进制,八进制和十六进制
 *
 */

public class Test1 extends JFrame{
	
	private JLabel label1,label2;
	private JTextField field1,field2;
	private JButton bt1,bt2,bt3;
	
	public Test1(){
		this.setSize(300,200);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		this.setLocation((int)((screenSize.getWidth()-frameSize.getWidth())/2), (int)(screenSize.getHeight()-frameSize.getHeight())/2);
		this.setTitle("进制转换");
		ImageIcon tp = new ImageIcon(Test1.class.getResource("/image/kobe.jpg"));
		this.setIconImage(tp.getImage());
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭同时关闭进程
		this.setResizable(false);//设置窗体不可放大
		
		this.setLayout(null);
		
		label1 = new JLabel("输入值");
		label1.setBounds(50, 20, 80, 20);
		this.add(label1);
		
		label2 = new JLabel("结果");
		label2.setBounds(200, 20, 50, 20);
		this.add(label2);
		
		field1 = new JTextField();
		field1.setBounds(40, 45, 60, 30);
		field1.setHorizontalAlignment(JTextField.CENTER);//设置文本框居中
		field1.setFont(new Font("微软雅黑",Font.ROMAN_BASELINE,14));
		this.add(field1);
		
		field2 = new JTextField();
		field2.setBounds(150, 45, 125, 30);
		field2.setHorizontalAlignment(JTextField.CENTER);//设置文本框居中
		field2.setFont(new Font("微软雅黑",Font.ROMAN_BASELINE,14));
		this.add(field2);
		
		bt1 = new JButton("二进制");
		bt1.setBounds(15, 100, 70, 35);
		bt1.setMargin(new Insets(0,0,0,0));//设置按钮的边缘空余部分为0
		this.add(bt1);
		bt1.addActionListener(new ActionListener() {//二进制转换
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				field2.setText(Integer.toBinaryString(Integer.parseInt(field1.getText())));
				
			}
		});
		
		bt2 = new JButton("八进制");
		bt2.setBounds(115, 100, 70, 35);
		bt2.setMargin(new Insets(0,0,0,0));//设置按钮的边缘空余部分为0
		this.add(bt2);
		bt2.addActionListener(new ActionListener() {//八进制转换
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				field2.setText(Integer.toOctalString(Integer.parseInt(field1.getText())));
				
			}
		});
		
		bt3 = new JButton("十六进制");
		bt3.setBounds(215, 100, 70, 35);
		bt3.setMargin(new Insets(0,0,0,0));//设置按钮的边缘空余部分为0
		this.add(bt3);
		bt3.addActionListener(new ActionListener() {//十六进制转换
			
			@Override
			public void actionPerformed(ActionEvent e) {
	
				field2.setText(Integer.toHexString(Integer.parseInt(field1.getText())));
				
			}
		});
		
		
		
	}
	
	public static void main(String[] args) {
		Test1 t1 = new Test1();
		t1.setVisible(true);
	}
	
}








